using ClassDemo.Model;
using ClassDemo.Model.Entity;
using ClassDemo.Repository;

namespace ClassDemo.Service;

/// <summary>
/// 学生服务实现
/// </summary>
public class StudentService : IStudentService
{
    private readonly IStudentRepository _studentRepository;

    public StudentService(IStudentRepository studentRepository)
    {
        _studentRepository = studentRepository;
    }

    /// <summary>
    /// 获取所有学生
    /// </summary>
    /// <returns>学生列表</returns>
    public async Task<List<Student>> GetAllStudentsAsync()
    {
        return await _studentRepository.GetAllAsync();
    }

    /// <summary>
    /// 根据ID获取学生
    /// </summary>
    /// <param name="id">学生ID</param>
    /// <returns>学生信息</returns>
    public async Task<Student?> GetStudentByIdAsync(int id)
    {
        return await _studentRepository.GetByIdAsync(id);
    }

    /// <summary>
    /// 根据学号获取学生
    /// </summary>
    /// <param name="studentNo">学号</param>
    /// <returns>学生信息</returns>
    public async Task<Student?> GetStudentByNoAsync(string studentNo)
    {
        return await _studentRepository.GetByStudentNoAsync(studentNo);
    }

    /// <summary>
    /// 分页获取学生列表
    /// </summary>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">页大小</param>
    /// <param name="keyword">搜索关键词</param>
    /// <returns>分页结果</returns>
    public async Task<(List<Student> Data, int Total)> GetStudentsPageAsync(int pageIndex, int pageSize, string? keyword = null)
    {
        if (string.IsNullOrWhiteSpace(keyword))
        {
            return await _studentRepository.GetPageListAsync(pageIndex, pageSize);
        }
        
        return await _studentRepository.GetPageListAsync(pageIndex, pageSize, 
            s => s.Name.Contains(keyword) || 
                 s.No.Contains(keyword) || 
                 s.Class.Contains(keyword) || 
                 s.Major.Contains(keyword));
    }

    /// <summary>
    /// 根据班级获取学生列表
    /// </summary>
    /// <param name="className">班级名称</param>
    /// <returns>学生列表</returns>
    public async Task<List<Student>> GetStudentsByClassAsync(string className)
    {
        return await _studentRepository.GetByClassAsync(className);
    }

    /// <summary>
    /// 根据专业获取学生列表
    /// </summary>
    /// <param name="major">专业</param>
    /// <returns>学生列表</returns>
    public async Task<List<Student>> GetStudentsByMajorAsync(string major)
    {
        return await _studentRepository.GetByMajorAsync(major);
    }

    /// <summary>
    /// 添加学生
    /// </summary>
    /// <param name="student">学生信息</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> AddStudentAsync(Student student)
    {
        // 数据验证
        if (string.IsNullOrWhiteSpace(student.No))
        {
            return (false, "学号不能为空");
        }
        
        if (string.IsNullOrWhiteSpace(student.Name))
        {
            return (false, "姓名不能为空");
        }

        // 检查学号是否已存在
        if (await _studentRepository.IsStudentNoExistsAsync(student.No))
        {
            return (false, "学号已存在");
        }

        try
        {
            var result = await _studentRepository.AddAsync(student);
            return result ? (true, "添加成功") : (false, "添加失败");
        }
        catch (Exception ex)
        {
            return (false, $"添加失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 更新学生信息
    /// </summary>
    /// <param name="student">学生信息</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> UpdateStudentAsync(Student student)
    {
        // 数据验证
        if (student.Id <= 0)
        {
            return (false, "学生ID无效");
        }
        
        if (string.IsNullOrWhiteSpace(student.No))
        {
            return (false, "学号不能为空");
        }
        
        if (string.IsNullOrWhiteSpace(student.Name))
        {
            return (false, "姓名不能为空");
        }

        // 检查学生是否存在
        var existingStudent = await _studentRepository.GetByIdAsync(student.Id);
        if (existingStudent == null)
        {
            return (false, "学生不存在");
        }

        // 检查学号是否被其他学生使用
        if (await _studentRepository.IsStudentNoExistsAsync(student.No, student.Id))
        {
            return (false, "学号已被其他学生使用");
        }

        try
        {
            var result = await _studentRepository.UpdateAsync(student);
            return result ? (true, "更新成功") : (false, "更新失败");
        }
        catch (Exception ex)
        {
            return (false, $"更新失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 删除学生
    /// </summary>
    /// <param name="id">学生ID</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> DeleteStudentAsync(int id)
    {
        if (id <= 0)
        {
            return (false, "学生ID无效");
        }

        // 检查学生是否存在
        var student = await _studentRepository.GetByIdAsync(id);
        if (student == null)
        {
            return (false, "学生不存在");
        }

        try
        {
            var result = await _studentRepository.DeleteByIdAsync(id);
            return result ? (true, "删除成功") : (false, "删除失败");
        }
        catch (Exception ex)
        {
            return (false, $"删除失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 批量删除学生
    /// </summary>
    /// <param name="ids">学生ID列表</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> DeleteStudentsAsync(List<int> ids)
    {
        if (ids == null || ids.Count == 0)
        {
            return (false, "请选择要删除的学生");
        }

        try
        {
            var result = await _studentRepository.DeleteAsync(s => ids.Contains(s.Id));
            return result > 0 ? (true, $"成功删除{result}个学生") : (false, "删除失败");
        }
        catch (Exception ex)
        {
            return (false, $"删除失败：{ex.Message}");
        }
    }
}